﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
         
            dg1.Checked = true;
            hienthi_diadiemdulich();
            InitLienHe();
        }
    }
    private void InitLienHe()
    {
        try
        {
            string sql = "select top 1 TEN_VAN_PHONG,DIA_CHI,DIEN_THOAI,FAX,HOT_LINE,EMAIL from van_phong where is_lien_he='Y'";
            DataTable dt = SQLConnectWeb.GetTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow r = dt.Rows[0];
                lblCompany.InnerText = r[VAN_PHONG.cl_TEN_VAN_PHONG].ToString().ToUpper();
                lblDiaChi.InnerHtml = "<br/> Địa chỉ: " + r[VAN_PHONG.cl_DIA_CHI];
                lblDienThoai.InnerHtml = "<br/>Điện thoại: " + r[VAN_PHONG.cl_DIEN_THOAI];
                if (r[VAN_PHONG.cl_HOT_LINE].ToString() != "")
                    lblHotLine.InnerHtml = "<br/>Hot line: " + r[VAN_PHONG.cl_HOT_LINE];
                if (r[VAN_PHONG.cl_FAX].ToString() != "")
                    lblFax.InnerHtml = "<br/>Fax: " + r[VAN_PHONG.cl_FAX];
                if (r[VAN_PHONG.cl_EMAIl].ToString() != "")
                    lblEmail.InnerHtml = (lblFax.InnerText == "" ? "<br/>" : "") + "Email: " + r[VAN_PHONG.cl_EMAIl];
            }
        }
        catch
        {
        }
    }
    
    public void hienthi_diadiemdulich()
    {
        try
        {

            String sql = @"select * from (
            select ROW_NUMBER() OVER (ORDER BY sap_xep) AS RowNumber,tt.tinh_thanh_id,ten_tinh_thanh
            from tinh_thanh tt 
            where exists(select top 1 dl.dia_diem_id from dia_diem_du_lich dl where dl.tinh_thanh=tt.tinh_thanh_id )) as tables where RowNumber <=10";
            
            DataTable table = SQLConnectWeb.GetData(sql);
            if (table == null || table.Rows.Count == 0)
            {

                xemthem.Visible = false;
            }
            else
            {
                diadiemdulich.DataSource = table;
                diadiemdulich.DataBind();

                String sqls = @"select * from (select ROW_NUMBER() OVER (ORDER BY isnull(sap_xep,9999)) AS RowNumber,tinh_thanh_id,ten_tinh_thanh 
            from tinh_thanh tt
            where exists(select top 1 dl.dia_diem_id from dia_diem_du_lich dl where dl.tinh_thanh=tt.tinh_thanh_id)) as tables where RowNumber >10";
                DataTable tables = SQLConnectWeb.GetData(sqls);

                diadiemdulich_more.DataSource = tables;
                diadiemdulich_more.DataBind();
                if (tables == null || tables.Rows.Count == 0)
                    xemthem.Visible = false;
            }

            String sql_news = string.Format(@"select TOP 10 a.ten_loai,b.bai_viet_id,b.ten_bai_viet,b.noi_dung,b.HINH_THUMBNAIL 
            from bai_viet b 
            left join  loai_bai_viet a on a.loai_bai_viet_id = b.loai_bai_viet 
            WHERE b.DUYET='Y' and  b.loai_bai_viet in ({0},{1},{2})
            ORDER BY b.NGAY_TAO",
            HelpConstEnum.LoaiBaiViet.TIN_TUC.GetHashCode(), 
            HelpConstEnum.LoaiBaiViet.THONG_BAO.GetHashCode(), 
            HelpConstEnum.LoaiBaiViet.LUU_BUT_KHACH_HANG.GetHashCode());
            DataTable table_news = SQLConnectWeb.GetData(sql_news);

            topnews.DataSource = table_news;
            topnews.DataBind();

            
            
        }
        catch
        {
        }
    }

    protected void btn_votes_Click(object sender, EventArgs e)
    {
        try
        {
            int id = 0;

            if (dg1.Checked == true)
            {
                id=1;
            }
            else if (dg2.Checked == true)
           {
               id = 2;
           }
            else if (dg3.Checked == true)
            {
                id = 3;
            }
            else if (dg4.Checked == true)
            {
                id = 4;
            }
            else if (dg5.Checked == true)
            {
                id = 5;
            }
            string updates = @"update danh_gia set diem=diem+1 where danh_gia_id=@danh_gia_id";
            SQLConnectWeb.ExecuteNonQuery(updates,
                                          "@danh_gia_id",id.ToString());

            Response.Redirect("ketquadg.aspx");

        }
        catch
        { 
        }
    }
   
}
